package com.zhouyu.exampleThread.config;

import com.zhouyu.exampleThread.builder.ThreadPoolBuilder;
import java.util.concurrent.Executor;
import lombok.extern.log4j.Log4j2;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;

/**
 * @Description:配置spring异步线程池，如果你需要异步
 */
@Configuration
@Log4j2
public class AsyncThreadPoolConfig implements AsyncConfigurer {
  @Override
  public Executor getAsyncExecutor() {
    return threadPoolTaskExecutor();
  }

  @Bean
  public Executor threadPoolTaskExecutor() {
    return ThreadPoolBuilder.builCommonThreadPool();
  }

  @Override
  public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
    return (throwable, method, params) -> {
      log.error("异步任务异常：方法：{} 参数：{}", method.getName(), params);
      log.error(throwable.getMessage(), throwable);
    };
  }
}
